package com.zxy.ziems.server.component.security.convert;

import com.zxy.btp.common.dto.ReturnUserInfo;
import com.zxy.btp.common.util.JsonUtils;
import com.zxy.btp.redis.utils.RedisUtil;
import com.zxy.btp.security.bean.CheckResult;
import com.zxy.btp.security.bean.TokenEntity;
import com.zxy.btp.security.convert.TokenConverter;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Time;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName JceTokenConverter
 * @Description
 * @Author 邓伟
 * @Date 2023/11/16 13:27
 * @Version 1.0
 */
@Component
public class JceTokenConverter implements TokenConverter {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Map<String, Object> enhance(Map<String, Object> map, ReturnUserInfo returnUserInfo) {
        return map;
    }

    @Override
    public CheckResult checkOther(HttpServletRequest httpRequest, TokenEntity tokenEntity) {
        //校验通过自动续期
        this.redisUtil.expire("access:" + tokenEntity.getAccessToken(), tokenEntity.getExpireIn(), TimeUnit.MINUTES);
        this.redisUtil.expire("auth:" + tokenEntity.getAccessToken(), tokenEntity.getExpireIn(), TimeUnit.MINUTES);
        this.redisUtil.expire("userInfo:" + tokenEntity.getAccessToken(), tokenEntity.getExpireIn(), TimeUnit.MINUTES);
      return CheckResult.SUCCESS;
    }
}
